package com.prs.crm.action.sms;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.Util;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.sms.SmsTimer;
import com.prs.crm.service.sms.SmsTimerService;

public class SmsTimerAction extends BaseAction{

	private SmsTimer smsTimer;
	private SmsTimerService smsTimerService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private String sendTime;
	
	private String acceptPhones;
	
	@Action("listSmsTimer")
	public String listSmsSendTimer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(SmsTimer.class);
		criteria.addOrder(Order.asc("sendTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSmsTimerService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editSmsTimer")
	public String editSmsSendTimer() {
		return SUCCESS;
	}
	
	@Action("viewSmsTimer")
	public String viewSmsSendTimer() {
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="submitSmsTimer",results={@Result(name="success",type="redirect",location="editSmsTimer"),
			@Result(name="error",type="chain",location="editSmsTimer")})
	public String submitSmsSendTimer() {
		if(acceptPhones == null || smsTimer.getMessage() == null) {
			this.addActionError("接收人和短信内容不能为空");
			return ERROR;
		}
		String[] phoneNos = acceptPhones.split("[;,；，]");
		try {
			Date timer = Util.formatDateStr(sendTime, "yyyy-MM-dd HH:mm");			
			this.getSmsTimerService().saveSmsTimer(phoneNos, this.getSmsTimer().getMessage(), timer);
			this.setPrompt("提交定时短信成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return SUCCESS;
	}
	
	@Action(value="removeSmsTimer",results={@Result(name="success",type="redirect",location="listSmsTimer"),
			@Result(name="error",type="chain",location="listSmsTimer")})
	public String removeSmsSendTimer() {
		if(ids == null) {
			this.addActionError("请选择要删除的定时短信");
			return ERROR;
		}
		this.getSmsTimerService().batchRemove(ids);
		return SUCCESS;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setAcceptPhones(String acceptPhones) {
		this.acceptPhones = acceptPhones;
	}

	public String getAcceptPhones() {
		return acceptPhones;
	}

	public String getSendTime() {
		return sendTime;
	}

	public void setSendTime(String sendTime) {
		this.sendTime = sendTime;
	}

	public void setSmsTimer(SmsTimer smsTimer) {
		this.smsTimer = smsTimer;
	}

	public SmsTimer getSmsTimer() {
		return smsTimer;
	}

	public void setSmsTimerService(SmsTimerService smsTimerService) {
		this.smsTimerService = smsTimerService;
	}

	public SmsTimerService getSmsTimerService() {
		return smsTimerService;
	}
}
